// VLine.cpp
//
#include "VLine.h"

#include "OpenGL/Renderer.h"
#include "OpenGL/Selection.h"
#include "OpenGL/OpenGL.h"

#include "Intersection.h"

// ------------------------------------------------------------------------------------------------
VLine::VLine()
{}

// ------------------------------------------------------------------------------------------------
VLine::VLine( const OpenGL::Math::dvec3 &p0, const OpenGL::Math::dvec3 &p1 )
: _p0(p0),
  _p1(p1)
{
	_boundingBox.extend( _p0 );
	_boundingBox.extend( _p1 );
}

// ------------------------------------------------------------------------------------------------
VObject::ObjectType VLine::type() const		{ return Line; }

// ------------------------------------------------------------------------------------------------
std::vector<OpenGL::Math::dvec3> VLine::snapPoints() const
{
	std::vector<OpenGL::Math::dvec3> pts;
	pts.push_back(_p0);
	pts.push_back(_p1);

	return pts;
}

// ------------------------------------------------------------------------------------------------
OpenGL::Math::dvec3 VLine::textPosition() const
{
	return _boundingBox.center();
}

// ------------------------------------------------------------------------------------------------
void VLine::draw( OpenGL::Renderer *r ) const
{
	OpenGL::Math::dvec3 o = r->origin();

	//glPushAttrib(GL_COLOR_BUFFER_BIT);
		//glLineWidth(2.f + (isHilighted() * .5f) );
		//glColor3ub(0,0,0);
		//glBegin(GL_LINES);
		//	glVertex2dv( OpenGL::Math::value_ptr(_p0-o) );
		//	glVertex2dv( OpenGL::Math::value_ptr(_p1-o) );
		//glEnd();

		glColor3fv( color().rgba() );
		glLineWidth(2.f);
		glBegin(GL_LINES);
			glVertex2dv( OpenGL::Math::value_ptr(_p0-o) );
			glVertex2dv( OpenGL::Math::value_ptr(_p1-o) );
		glEnd();
	//glPopAttrib();	
}

// ------------------------------------------------------------------------------------------------
void VLine::drawForSelection( OpenGL::Renderer *r ) const
{
	OpenGL::Selection::Name name( _id );

	OpenGL::Math::dvec3 o = r->origin();

	glLineWidth(2.f);
	glBegin(GL_LINES);
		glVertex2dv( OpenGL::Math::value_ptr(_p0-o) );
		glVertex2dv( OpenGL::Math::value_ptr(_p1-o) );
	glEnd();
}

// ------------------------------------------------------------------------------------------------
bool VLine::intersects( const OpenGL::Math::box3 &b ) const 
{
	// test p0-p1 against box edges
	bool intersect = false;

	const OpenGL::Math::dvec2 p0( _p0.x,_p0.y );
	const OpenGL::Math::dvec2 p1( _p1.x,_p1.y );

	intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.minPoint().x,b.minPoint().y), OpenGL::Math::dvec2(b.minPoint().x,b.maxPoint().y) ); // left
	intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.minPoint().x,b.maxPoint().y), OpenGL::Math::dvec2(b.maxPoint().x,b.maxPoint().y) ); // top
	intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.maxPoint().x,b.maxPoint().y), OpenGL::Math::dvec2(b.maxPoint().x,b.minPoint().y) ); // right
	intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.maxPoint().x,b.minPoint().y), OpenGL::Math::dvec2(b.minPoint().x,b.minPoint().y) ); // bottom

	return intersect;
}
